package com.coolbi.onsupai.core.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.onsupai.core.biz.CustomerBiz;
import com.coolbi.onsupai.core.dao.CustomerDao;
import com.coolbi.onsupai.core.entity.Customer;
import com.coolbi.onsupai.core.exception.ServiceException;

public class CustomerBizImpl implements CustomerBiz {

	@Autowired
	private CustomerDao customerDao;
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	
	@Override
	public Customer save(Customer obj) {
		try {
			customerDao.save(obj);
		} catch (Exception e) {
			throw new ServiceException("添加客户发生错误！", e);
		}
		return obj;
	}

	@Override
	public void delete(long id) {
		try {
			customerDao.delete(id);
		} catch (Exception e) {
			throw new ServiceException("删除客户发生错误！", e);
		}
	}

	@Override
	public void update(Customer obj) {
		try {
			Customer customer = customerDao.get(obj.getId());
			customer.setUserName(obj.getUserName());
			customer.setRealName(obj.getRealName());
			customer.setGender(obj.getGender());
			customer.setPhoneNumber(obj.getPhoneNumber());
			customer.setTelephone(obj.getTelephone());
			customer.setEmail(obj.getEmail());
			customer.setQqNumber(obj.getQqNumber());
			customer.setWangWang(obj.getWangWang());
			customer.setBirthDay(obj.getBirthDay());
			customer.setAddress(obj.getAddress());
			customer.setHabit(obj.getHabit());
			if (customerDao.update(customer) < 0) {
				throw new ServiceException("修改客户发生失败！");
			}
		} catch (Exception e) {
			throw new ServiceException("修改客户发生错误！", e);
		}
	}

	@Override
	public Customer get(long id) {
		return customerDao.get(id);
	}

	@Override
	public List<Customer> list() {
		return customerDao.list();
	}

	@Override
	public List<Customer> listByPage(int offset, int limit) {
		return customerDao.listByPage(offset, limit);
	}

	@Override
	public List<Customer> find(Map<String, Object> parameter) {
		return customerDao.find(parameter);
	}

	@Override
	public List<Customer> findByPage(Map<String, Object> parameter, int offset,
			int limit) {
		return customerDao.findByPage(parameter, offset, limit);
	}

	@Override
	public List<Customer> search(Map<String, Object> parameter, int offset, int limit) {
		return customerDao.search(parameter, offset, limit);
	}

	@Override
	public int count() {
		return customerDao.count();
	}

	@Override
	public int countByKeyWord(Map<String, Object> parameter) {
		return customerDao.countByKeyWord(parameter);
	}

	@Override
	public int count(Map<String, Object> parameter) {
		return customerDao.count(parameter);
	}

	@Override
	public List<Customer> findByKeyWord(String searchType, String keyWord, int offset, int limit) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put(searchType, keyWord);
		return customerDao.search(parameter, offset, limit);
	}

	@Override
	public int countBykeyWord(String searchType, String keyWord) {
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put(searchType, keyWord);
		return customerDao.countByKeyWord(parameter);
	}

	@Override
	public boolean checkTel(String tel) {
		return customerDao.countByTel(tel) > 0;
	}

	@Override
	public boolean checkEmail(String email) {
		return customerDao.countByEmail(email) > 0;
	}

	@Override
	public boolean checkUserName(String userName) {
		return customerDao.countByUserName(userName) > 0;
	}

	@Override
	public boolean checkQQNumber(String qqNumber) {
		return customerDao.countByQQNumber(qqNumber) > 0;
	}
}
